package com.sanid.wqb;

import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;

import com.google.wave.api.AbstractRobotServlet;
import com.google.wave.api.Blip;
import com.google.wave.api.Element;
import com.google.wave.api.ElementType;
import com.google.wave.api.RobotMessageBundle;
import com.google.wave.api.StyleType;
import com.google.wave.api.StyledText;
import com.google.wave.api.TextView;
import com.google.wave.api.Wavelet;

@SuppressWarnings("serial")
public class WaveQuizBotServlet extends AbstractRobotServlet {

	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger("waveqbot");
	
	private Map<String, Channel> channels = new HashMap<String, Channel>();

	@Override
	public void processEvents(RobotMessageBundle bundle) {
		logger.info("processEvents");
		
		//Channel channel = getChannel(bundle);
		//channel.processEvents(bundle);
		if(bundle.wasSelfAdded()) {
			Wavelet wt = bundle.getWavelet();
			Blip blip = wt.appendBlip();
			TextView text = blip.getDocument();
			text.appendElement(new Element(ElementType.BUTTON));
			text.appendMarkup("MyMarkup");
		}
	}

	/**
	 * @param wavelet
	 * @return
	 */
	private synchronized Channel getChannel(RobotMessageBundle bundle) {
		Wavelet wavelet = bundle.getWavelet();
		String waveletUniqueId = wavelet.getWaveId() + "/" + wavelet.getWaveletId();
		Channel channel = channels.get(waveletUniqueId);
		if(channel == null) {
			channel = new Channel(bundle);
			channels.put(waveletUniqueId, channel);
			logger.info("Channel created ("+waveletUniqueId+")");
		} else {
			logger.info("Channel reused ("+waveletUniqueId+")");
		}
		return channel;
	}
}
